package com.wechat.weibo.service;

import com.wechat.weibo.dao.*;
import com.wechat.weibo.entity.Follows;
import com.wechat.weibo.entity.User;
import com.wechat.weibo.entity.WeiBo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WeiboService {
    @Autowired(required = false)
    WeiboDao weiboDao;
    @Autowired(required = false)
    LikeDao likeDao;
    @Autowired(required = false)
    PickDao pickDao;
    @Autowired(required = false)
    CommentDao commentDao;
    @Autowired(required = false)
    UserDao userDao;


    public int delWeibo(User user,int weibo_id){
        userDao.delWeibo(user);
        weiboDao.delWeibo(weibo_id);
        commentDao.delWeibo(weibo_id);
        likeDao.delWeibo(weibo_id);
        return 0;
    }
    public WeiBo showSingle(String selfID,int weibo_id){
        WeiBo weiBo=weiboDao.showSingle(weibo_id);
        rewriteWeiBo(weiBo,selfID);
        return weiBo;
    }
    public int postWeibo(User user, String content){
        Date date=new Date();
        date.setTime(date.getTime()+8*60*60*1000);
        userDao.addWeibo(user);
        weiboDao.post(user,content,date);
        return 0;
    }

    public List<WeiBo> getWeiBo(String selfID,String hostID){
        List<WeiBo> list= weiboDao.showWeiBoById(hostID);
        for (WeiBo weiBo:list
             ) {
            rewriteWeiBo(weiBo,selfID);
        }
        return list;
    }
    public List<WeiBo> getAllWeiBo(String selfID){
        List<WeiBo> list= weiboDao.showAllWeiBo();
        for (WeiBo weiBo:list
        ) {
            rewriteWeiBo(weiBo,selfID);
        }
        return list;
    }
    public void rewriteWeiBo(WeiBo weiBo, String selfID){
        if(weiBo==null)return;
        if(selfID!=null&&likeDao.getLike(selfID,weiBo.getWeibo_id())!=null)weiBo.setIs_like(true);
        User user=userDao.getUser(weiBo.getHost_openid());
        if(user!=null){
            weiBo.setHost_name(user.getUserName());
            weiBo.setHost_img(user.getImg());
        }
    }
    public List<WeiBo> getFollowWeiBo(String selfID){
        List<Follows> followsList=pickDao.getPick(selfID);
        List<WeiBo> weiBoList=new LinkedList<>();
        for (Follows follow:followsList
             ) {
            List<WeiBo> tmpList= weiboDao.showWeiBoById(follow.getMain_id());
            for (WeiBo weiBo:tmpList
            ) {
                rewriteWeiBo(weiBo,selfID);
                weiBoList.add(weiBo);
            }
        }
        List<WeiBo> tmpList= weiboDao.showWeiBoById(selfID);
        for (WeiBo weiBo:tmpList
        ) {
            rewriteWeiBo(weiBo,selfID);
            weiBoList.add(weiBo);
        }
        weiBoList.sort((o1, o2) -> {
            if(o1.getPost_time().equals(o2.getPost_time()))return 0;
            if(o1.getPost_time().before(o2.getPost_time()))return 1;
            return -1;
        });
        return weiBoList;
    }

    public int addLike(String selfID,int weibo_id){
        if(likeDao.getLike(selfID,weibo_id)!=null)return -1;
        if(weiboDao.showSingle(weibo_id)==null)return 1;
        likeDao.addLike(selfID,weibo_id);
        weiboDao.addLike(weibo_id);
        return 0;
    }
    public int delLike(String selfID,int weibo_id){
        if(likeDao.getLike(selfID,weibo_id)==null)return -1;
        if(weiboDao.showSingle(weibo_id)==null)return 1;
        likeDao.delLike(selfID,weibo_id);
        weiboDao.delLike(weibo_id);
        return 0;
    }
}
